using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MicroMsg.Manager
{
	public class SnsPageMgr
	{
		private const int COUNT_PER_PAGE = 40;
		public const int GET_LAST_SNS_IMG_NUM = 3;
		private static SnsPageSource timeLineSrc;
		private static SnsPageSource ownerSrc;
		private static SnsPageSource othersSrc;
		private static List<TimeLineObjStyle> timeLineObjectStyleList;
		private static long timeStart;
		private static Dictionary<string, SnsPageSource> mapPageSrc;
		private static List<TimeLineObjStyle> LastObjectStyleList;
		public static string TimeLineUserName
		{
			get
			{
				return SnsPageStorage.getTimeLineUserName();
			}
		}
		static SnsPageMgr()
		{
			List<TimeLineObjStyle> list = new List<TimeLineObjStyle>();
			list.Add(TimeLineObjStyle.MMSNS_OBJECT_PHOTO);
			list.Add(TimeLineObjStyle.MMSNS_OBJECT_TEXT);
			list.Add(TimeLineObjStyle.MMSNS_OBJECT_FEED);
			list.Add(TimeLineObjStyle.MMSNS_OBJECT_MUSIC);
			list.Add(TimeLineObjStyle.MMSNS_OBJECT_BACKGROUND);
			list.Add(TimeLineObjStyle.MMSNS_OBJECT_WXSIGN);
			SnsPageMgr.timeLineObjectStyleList = list;
			SnsPageMgr.mapPageSrc = new Dictionary<string, SnsPageSource>();
			List<TimeLineObjStyle> list2 = new List<TimeLineObjStyle>();
			list2.Add(TimeLineObjStyle.MMSNS_OBJECT_PHOTO);
			list2.Add(TimeLineObjStyle.MMSNS_OBJECT_BACKGROUND);
			SnsPageMgr.LastObjectStyleList = list2;
			AccountMgr.registerLoginNotify(new onAccountLoginCallback(SnsPageMgr.onLogin));
			if (AccountMgr.isLogin)
			{
				SnsPageMgr.onLogin();
			}
		}
		public static void init()
		{
		}
		private static void onLogin()
		{
			SnsPageMgr.timeLineSrc = null;
			SnsPageMgr.ownerSrc = null;
			SnsPageMgr.othersSrc = null;
		}
		public static SnsPageSource getPageSrc(string userName)
		{
			SnsPageMgr.removeCache(userName);
			if (userName == SnsPageMgr.TimeLineUserName)
			{
				if (SnsPageMgr.timeLineSrc == null)
				{
					SnsPageMgr.timeLineSrc = new SnsPageSource(userName);
					SnsPageMgr.timeLineSrc.setAllowStyle(SnsPageMgr.timeLineObjectStyleList);
				}
				return SnsPageMgr.timeLineSrc;
			}
			if (userName == AccountMgr.strUsrName)
			{
				if (SnsPageMgr.ownerSrc == null)
				{
					SnsPageMgr.tryVerifySentSnsobject();
					SnsPageMgr.tryVerifyDeleteSnsobject();
					SnsPageMgr.ownerSrc = new SnsPageSource(userName);
				}
				return SnsPageMgr.ownerSrc;
			}
			if (SnsPageMgr.othersSrc != null && SnsPageMgr.othersSrc.strUserName == userName)
			{
				return SnsPageMgr.othersSrc;
			}
			SnsPageMgr.othersSrc = new SnsPageSource(userName);
			SnsPageMgr.othersSrc.checkUserType(true);
			return SnsPageMgr.othersSrc;
		}
		public static void ClearAll()
		{
			StorageMgr.snsPage.clearAll();
			SnsPageMgr.timeLineSrc = null;
			SnsPageMgr.ownerSrc = null;
			SnsPageMgr.othersSrc = null;
			SnsPageMgr.mapPageSrc.Clear();
		}
		public static bool needShowSendingObj(string userName)
		{
			return userName == SnsPageMgr.TimeLineUserName || userName == AccountMgr.strUsrName;
		}
		public static bool needShowSentObj(string userName)
		{
			return userName == AccountMgr.strUsrName;
		}
		public static void notifySendingObjChanged()
		{
			if (SnsPageMgr.timeLineSrc != null)
			{
				SnsPageMgr.timeLineSrc.onSendingObjChanged();
			}
			if (SnsPageMgr.ownerSrc != null)
			{
				SnsPageMgr.ownerSrc.onSendingObjChanged();
			}
		}
		public static void notifySnsRecycled()
		{
			if (SnsPageMgr.timeLineSrc != null)
			{
				SnsPageMgr.timeLineSrc.onSnsRecyced();
			}
			if (SnsPageMgr.ownerSrc != null)
			{
				SnsPageMgr.ownerSrc.onSnsRecyced();
			}
			if (SnsPageMgr.othersSrc != null)
			{
				SnsPageMgr.othersSrc.onSnsRecyced();
			}
		}
		public static void notifySnsDelete()
		{
			if (SnsPageMgr.timeLineSrc != null)
			{
				SnsPageMgr.timeLineSrc.onSnsDelete();
			}
			if (SnsPageMgr.ownerSrc != null)
			{
				SnsPageMgr.ownerSrc.onSnsDelete();
			}
			if (SnsPageMgr.othersSrc != null)
			{
				SnsPageMgr.othersSrc.onSnsDelete();
			}
		}
		private static ulong getFirstObjectID(string userName)
		{
			SnsPage snsPage = StorageMgr.snsPage.method_1(userName);
			if (snsPage == null)
			{
				return 18446744073709551615uL;
			}
			ulong first = snsPage.regionInfo.getFirst();
			if (first == 0uL)
			{
				return 18446744073709551615uL;
			}
			return first;
		}
		private static void tryVerifySentSnsobject()
		{
			List<ulong> sentObjectID = SnsInfoMgr.getSentObjectID();
			if (sentObjectID != null && sentObjectID.get_Count() >= 20)
			{
				ulong firstObjectID = SnsPageMgr.getFirstObjectID(SnsPageMgr.TimeLineUserName);
				ulong firstObjectID2 = SnsPageMgr.getFirstObjectID(AccountMgr.strUsrName);
				ulong min_id = Math.Min(firstObjectID, firstObjectID2);
				SnsInfoMgr.delSentObjectID(Enumerable.ToList<ulong>(Enumerable.Where<ulong>(sentObjectID, (ulong ulong_0) => ulong_0 <= min_id)));
				return;
			}
		}
		private static void deleteObjectIDS(string userName, List<ulong> ids_del)
		{
			SnsPage snsPage = StorageMgr.snsPage.method_1(userName);
			if (snsPage == null)
			{
				return;
			}
			snsPage.regionInfo.removeRange(ids_del);
			snsPage.regionsInfoFlush();
			StorageMgr.snsPage.modify(snsPage);
		}
		private static void tryVerifyDeleteSnsobject()
		{
			List<ulong> list = SnsAsyncMgr.getDeleteIDS();
			if (list.get_Count() <= 0)
			{
				return;
			}
			list = Enumerable.ToList<ulong>(Enumerable.Except<ulong>(list, SnsAsyncMgr.getDeletingIDS()));
			if (list.get_Count() <= 0)
			{
				return;
			}
			SnsPageMgr.deleteObjectIDS(SnsPageMgr.TimeLineUserName, list);
			SnsPageMgr.deleteObjectIDS(AccountMgr.strUsrName, list);
			SnsAsyncMgr.removeDeleteIDS(list);
		}
		public static void setTimeStamp(string step)
		{
			if (step == null)
			{
				SnsPageMgr.timeStart = DebugEx.getTimeSpan(0L);
			}
		}
		private static void removeCache(string strUserName)
		{
			if (SnsPageMgr.mapPageSrc.ContainsKey(strUserName))
			{
				SnsPageMgr.mapPageSrc.Remove(strUserName);
			}
		}
		public static List<TloMedia> getLastSnsImage(string strUserName, bool bNetSync = true)
		{
			SnsPageMgr.setTimeStamp(null);
			if (string.IsNullOrEmpty(strUserName))
			{
				return null;
			}
			if (SnsPageMgr.mapPageSrc.get_Count() > 50)
			{
				SnsPageMgr.mapPageSrc.Clear();
			}
			SnsPageSource snsPageSource;
			if (SnsPageMgr.mapPageSrc.ContainsKey(strUserName))
			{
				snsPageSource = SnsPageMgr.mapPageSrc.get_Item(strUserName);
			}
			else
			{
				snsPageSource = new SnsPageSource(strUserName);
				snsPageSource.setAllowStyle(SnsPageMgr.LastObjectStyleList);
				snsPageSource.setResponeCallback(new Action<SnsPageSource>(SnsPageMgr.onPageResponse));
				SnsPageMgr.mapPageSrc.set_Item(strUserName, snsPageSource);
				if (snsPageSource.count() <= 0)
				{
					snsPageSource.bCacheMode = true;
				}
			}
			if (bNetSync && !snsPageSource.isPageRefreshing())
			{
				snsPageSource.refreshFirsPage();
			}
			return SnsPageMgr.getLastImageFromLocal(snsPageSource);
		}
		private static void onPageResponse(SnsPageSource pageSrc)
		{
			if (pageSrc == null || string.IsNullOrEmpty(pageSrc.strUserName))
			{
				return;
			}
			if (!SnsPageMgr.mapPageSrc.ContainsKey(pageSrc.strUserName))
			{
				return;
			}
			List<TloMedia> list = pageSrc.bCacheMode ? SnsPageMgr.getLastImageFromCache(pageSrc) : SnsPageMgr.getLastImageFromLocal(pageSrc);
			if (list != null && list.get_Count() > 0)
			{
				if (pageSrc.bCacheMode)
				{
					SnsPageMgr.setTimeStamp("download  image start");
					using (List<TloMedia>.Enumerator enumerator = list.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							TloMedia current = enumerator.get_Current();
							SnsMediaMgr.getImg(new snsMediaDownloadInfo
							{
								string_0 = current.strThumb,
								urlType = SnsUrlType.MMSNS_URL_CDN,
								snsObjID = current.nID,
								priority = SnsPriority.HIGH
							}, true);
						}
					}
				}
				EventCenter.postEvent(EventConst.ON_MGR_SNSPAGE_GET_LAST_IMAGE, pageSrc.strUserName, list);
				return;
			}
			EventCenter.postEvent(EventConst.ON_MGR_SNSPAGE_GET_LAST_IMAGE, pageSrc.strUserName, null);
		}
		private static List<TloMedia> getLastImageFromLocal(SnsPageSource pageSrc)
		{
			List<SnsInfo> list = pageSrc.getList(0, 3);
			if (list != null && list.get_Count() > 0)
			{
				List<byte[]> xmlList = Enumerable.ToList<byte[]>(Enumerable.Select<SnsInfo, byte[]>(list, (SnsInfo snsInfo_0) => snsInfo_0.bytesObjectDesc));
				return SnsPageMgr.getLastImage(xmlList, 3);
			}
			return null;
		}
		private static List<TloMedia> getLastImageFromCache(SnsPageSource pageSrc)
		{
			List<SnsObject> cacheList = pageSrc.getCacheList();
			if (cacheList != null && cacheList.get_Count() > 0)
			{
				List<byte[]> xmlList = Enumerable.ToList<byte[]>(Enumerable.Select<SnsObject, byte[]>(cacheList, (SnsObject snsObject_0) => snsObject_0.ObjectDesc.Buffer.ToByteArray()));
				return SnsPageMgr.getLastImage(xmlList, 3);
			}
			return null;
		}
		private static List<TloMedia> getLastImage(List<byte[]> xmlList, int int_0 = 3)
		{
			if (xmlList == null || xmlList.get_Count() <= 0)
			{
				return null;
			}
			List<TloMedia> list = new List<TloMedia>();
			List<TloMedia> result;
			using (List<byte[]>.Enumerator enumerator = xmlList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					byte[] current = enumerator.get_Current();
					TimeLineObject timeLineObject = TimeLineObject.ParseFrom(current);
					if (timeLineObject != null && timeLineObject != null && timeLineObject.content != null && timeLineObject.content.mediaList != null && timeLineObject.content.mediaList.get_Count() != 0 && (timeLineObject.content.nStyle == 1 || timeLineObject.content.nStyle == 7))
					{
						using (List<TloMedia>.Enumerator enumerator2 = timeLineObject.content.mediaList.GetEnumerator())
						{
							while (enumerator2.MoveNext())
							{
								TloMedia current2 = enumerator2.get_Current();
								list.Add(current2);
								if (int_0 == list.get_Count())
								{
									result = list;
									return result;
								}
							}
						}
					}
				}
				goto IL_F4;
			}
			return result;
			IL_F4:
			if (list.get_Count() <= 0)
			{
				return null;
			}
			return list;
		}
		public static SnsInfo getLastTimeLineImage()
		{
			SnsPageSource snsPageSource = new SnsPageSource(SnsPageMgr.TimeLineUserName);
			snsPageSource.setAllowStyle(SnsPageMgr.LastObjectStyleList);
			List<SnsInfo> list = snsPageSource.getList(0, 1);
			if (list != null && list.get_Count() > 0)
			{
				return list.get_Item(0);
			}
			return null;
		}
	}
}
